﻿using System.Collections.Generic;
using Pumpkin.Utils;

namespace Pumpkin.FrameBase
{
    public class CrossMethod
    {
        public string Name;
        public int ParamCount;
    }

    public class CrossMethods : Singleton<CrossMethods>
    {
        private Dictionary<string, List<CrossMethod>> methods = new Dictionary<string, List<CrossMethod>>();

        public Dictionary<string, List<CrossMethod>> Methods
        {
            get
            {
                return methods;
            }
        }

        public void Add(string clsName, string methodName, int paramCount)
        {
            var functions = methods.GetOrDefault(clsName);
            if (functions == null)
            {
                functions = new List<CrossMethod>();
                methods.Add(clsName, functions);
            }

            functions.Add(new CrossMethod()
            {
                Name = methodName,
                ParamCount = paramCount,
            });
        }

        public void RegisterMethods(CrossDomainInfo info)
        {
            var functions = methods.GetOrDefault(info.ClassName);
            if (functions == null || functions.Count == 0)
            {
                return;
            }
            foreach (var method in functions)
            {
                info.RegisterMethods(method.Name, method.ParamCount);
            }
        }

        public bool IsClassRegisted(string className)
        {
            return methods.ContainsKey(className);
        }
    }
}
